package com.xzx.nio;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.nio.IntBuffer;

/**
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/9/23 11:16
 */
public class BufferTest {

    @BeforeEach
    public void init(){
        System.out.println("init");
    }

    @AfterEach
    public void destroy(){
        System.out.println("destroy");
    }

    @Test
    public void test1(){
        IntBuffer buffer = IntBuffer.allocate(5);
        for (int i = 0; i < buffer.capacity(); i++) {
            int j = 2 * i + 1;
            buffer.put(j);
        }
        System.out.println(buffer.limit() + " " + buffer.capacity() + " " + buffer.position());
        // 反转
        buffer.flip();
        while (buffer.hasRemaining()){
            int i = buffer.get();
            System.out.println(i);
        }
        System.out.println(buffer.limit() + " " + buffer.capacity() + " " + buffer.position());
        // 倒带，重置position的位置，可以重新读
        buffer.rewind();


    }
}
